export const getColumns = (_dataHead, _dataBody) => {
  const render = (v, r) => {
    if (r.indicatorsName === '费用占比' && !!v) return `${v}%`;
    return v;
  };
  // 计算合并行、合并列
  let spanMap = {
    groupName: {},
    provinceName: {},
    stationName: {},
    actualQuantity: {},
  };
  for (const key in spanMap) {
    const list = _dataBody.map((x) => x[key]);
    for (let i = 0; i < list.length; i++) {
      const val = list[i];
      let count = 1;
      let temp = {};
      if (key === 'groupName' && val === '合计') {
        temp.colSpan = 3;
      }
      if (key === 'provinceName' && val === '发电集团小计') {
        temp.colSpan = 2;
      }
      while (list[i + count] === val) {
        count++;
      }
      if (count > 1) {
        temp.rowSpan = count;
      }
      spanMap[key][i] = temp;
      i += count - 1;
    }
  }
  const getCellPropsFn = (key) => {
    return (v, r, i) => ({ ...(spanMap[key][i] || {}) });
  };

  const columns = [
    {
      code: 'groupName',
      name: '发电集团',
      width: 100,
      align: 'center',
      lock: true,
      getCellProps: getCellPropsFn('groupName'),
    },
    {
      code: 'provinceName',
      name: '省份',
      width: 100,
      align: 'center',
      lock: true,
      getCellProps: getCellPropsFn('provinceName'),
    },
    {
      code: 'stationName',
      name: '交易单元',
      width: 100,
      align: 'center',
      lock: true,
      getCellProps: getCellPropsFn('stationName'),
    },
    {
      name: '交易二级分类',
      align: 'center',
      lock: true,
      children: [
        {
          code: 'actualQuantity',
          name: '现货月上网电量',
          width: 100,
          align: 'center',
          lock: true,
          render,
          getCellProps: getCellPropsFn('actualQuantity'),
        },
        {
          code: 'indicatorsName',
          name: '指标名称',
          width: 100,
          align: 'center',
          lock: true,
        },
      ],
    },
    {
      name: '其他非交易',
      align: 'center',
      lock: true,
      children: _dataHead.map((x) => ({
        code: x.field,
        name: x.name,
        width: 100,
        align: 'center',
        render,
      })),
    },
    { code: 'total', name: '合计', width: 100, align: 'center', render },
  ];
  return columns;
};
